from urllib.parse import urlencode
from urllib.request import urlopen, Request

if __name__ == '__main__':
    base_url = "https://www.baidu.com/s?"

    keyword = {
        "wd": "陕西交通职业技术学院"
    }

    # https://www.baidu.com/s?wd=%E9%99%95%E8%A5%BF%E4%BA%A4%E9%80%9A%E8%81%8C%E4%B8%9A%E6%8A%80%E6%9C%AF%E5%AD%A6%E9%99%A2
    code = urlencode(query=keyword)
    print(code)
    url = base_url + code
    print(url)
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
    }

    # 自定义请求头
    res = Request(url=url, headers=headers)
    # 使用请求对象发送请求
    resp = urlopen(res)
    print(type(resp))
    # 获取 HTTP 响应状态码
    print(resp.getcode())

    # 读响应内容
    content = resp.read().decode("utf-8")
    print(content)